#include <iostream>
#include "fraction.h"
using namespace std;

Fraction::Fraction() //无参构造函数
{
  a = 0;
  b = 1;
}

//两个整数为参数的构造函数
Fraction::Fraction(int a, int b)
{
  set(a, b); //调用成员函数
}

//拷贝构造函数
Fraction::Fraction(Fraction &c)
{
  a = c.a;
  b = c.b;
}

//设置分母分子
void Fraction::set(int a, int b)
{
  this->a = a;
  this->b = b;
}

//显示分数
void Fraction::show()
{
  cout << a << "/" << b;
}

//分数相加，本类对象加u
Fraction Fraction::add(Fraction u)
{
  int tmp;
  Fraction v;

  v.a = a * u.b + b * u.a;
  v.b = b * u.b;
  tmp = divisor(v.a, v.b);
  v.a = v.a / tmp;
  v.c = v.c / tmp;
  return v;
}

//计算公约数，私有成员
int Fraction::divisor(int p, int q)
{

  int r;
  if (p < q)
  {
    int tmp;
    tmp = p;
    p = q;
    q = tmp;
  }
  r = p % q;
  while (r != 0)
  {
    p = q;
    q = r;
    r = p % q;
  }
  return q;
}